<?php
	include("gen.php");
	$cmd=get_datan("cmd");
	switch($cmd){
		case 1:
			//get one community member based on id
			get_community_members();
			break;
		case 2:
		     update_community_members();
			break;
		case 3:
		     //get all community members and return as array
            break;
        case 4:
            add_community_member();
            break;
        case 5:
            delete_community_member();
            break;
		default:
			echo "{";
			echo jsonn("result",0). ",";
			echo jsons("message","unknown command");
			echo "}";
	}
	
	
	function get_community_members(){
		include_once("community_members.php");
		
		$id=$id=get_datan("id");
		$v=new community_members ();
		$row=$v->get_community_members($id);
		if(!$row){
			echo "{";
			echo jsonn("result",0). ",";
			echo jsons("message","Community members not found");
			echo "}";
			return;
		}
		
		echo "{";
			echo jsonn("result",1) .",";
			echo '"community":{';
			echo jsonn("id",$id).",";
			
			echo jsons("community_member_id",$row['community_member_id']).",";
			//name
			echo jsons("card_number",$row['card_number']).",";
			//fullname
			echo jsons("fullname",$row['fullname']).",";
			//age
			echo jsonn("age",$row['age']).",";
			//birthdate
			echo jsons("birthdate",$row['birthdate']).",";
			//gender
			echo jsons("gender",$row['gender']).",";
			//household
			echo jsonn("household_id",$row['household_id']).",";
			//community
			echo jsonn("community_id",$row['community_id']).",";
			//registration
			echo jsons("registration_date",$row['registration_date']).",";
			//up_date
			echo jsons("up_datedate",$row['up_datedate']).",";
			//device
			echo jsonn("device_id",$row['device_id']);
			echo "}";
		echo "}";
	}

function add_community_member()
{

    $card_number = get_data('cd');
    $fullname = get_data('fn');
    $age = get_data('ag');
    $birthdate = get_data('bdate');
    $gender = get_data('gd');
    $household_id = get_data('hs');
    $community_id= get_data('cid');
    $registration_date = get_data('reg');
    $device_id = get_datan('dv');


    include("community_members.php");
    $v = new community_members();
    if(!$v-> add_community_member($card_number,$fullname,$age,$birthdate,
        $gender,$household_id,$community_id,$registration_date,$device_id))
    {
        echo '{"result": 0,"message":"adding failed"}';
    }
    else
    {
        echo '{"result": 1,"message":"adding successful"}';
    }
}
	
	function update_community_members()
    {
	include_once("community_members.php");
	       $community_member_id=get_datan('cmid');
		   $card_number=get_data('cn');
		   $fullname=get_data('fn');
		   $age=get_datan('a');
		  $birthdate=get_data('bd');
		  $gender=get_data('g');
		  $household_id=get_datan('hi');
		  $community_id=get_datan('ci');
		  $registration_date=get_datan('rd');
		  $up_datedate=get_datan('ud');
		  $device_id=get_datan('di');
		  
		   $v = new community_members();
		   $row = $v->update_community_members($community_member_id,$card_number,$fullname,$age,
           $birthdate,$gender,$household_id,$community_id,$registration_date, $up_datedate,$device_id);
		   if(!$row){
		      //return error message
			  echo "{";
			echo jsonn("result",0). ",";
			echo jsons("message","id not correct");
			echo "}";
			return;
			  
			  }


		}

    function delete_community_member()
    {
        include_once("community_members.php");

        $community_member_id=get_datan('cmid');

        $v = new community_members();

        if(!$v->delete_community_member($community_member_id))
        {
        //return error message
        echo "{";
        echo jsonn("result",0). ",";
        echo jsons("message","id not correct");
        echo "}";
        return;

    }

}

?>